import {baseUrl, defaultPost, get, post} from "./axios";
import utils from "./utils";
import {Message} from "element-ui";

const url = {
    submitUrl: '/v1/design/upsert',
    queryUrl: '/v1/design/findMany',
    processUrl: '/v1/design/process/:api_name',
    remCacheUrl: '/v1/api/cache_response/:action/:id',
    exportPostmanFileUrl: '/v1/api/postman/export'
}

/**
 * submitHandler designer表单提交
 * @param self
 * @returns {boolean}
 */
export function submitHandler(self) {
    let data = self.form;
    if (utils.isEmpty(data['group_name'],
        data['api_name'],
        data['data']['method'],
        data['data']['url'],
        data['data']['Content-Type'],
    )) {
        Message({
            message: '请求参数缺失，请检查',
            type: "error"
        })
    } else {
        if (data._id === "") {
            delete data._id
        }
        post(url.submitUrl, data, {}, true, false).then((response) => {
            if (response.data.code === 0) {
                Message({
                    message: 'designer api设计图纸更新成功',
                    type: "success",
                })
                self.closeForm()
            } else {
                Message({
                    message: response.data.msg,
                    type: "warning",
                })
            }
        }).catch((error) => {
        })
    }
}

/**
 *
 * @param filter
 * @param page
 * @param pageSize
 * @returns {Promise<AxiosResponse<any>>}
 */
export function queryHandler(filter = {}, page = 1, pageSize = 10) {
    let data = {
        filter: filter,
        page: page,
        page_size: pageSize,
    }
    return post(url.queryUrl, data, {}, true, false)
}

export function processHandler(api_name = "", body = {}) {
    let processUrl = url.processUrl.replace(":api_name", api_name)
    return defaultPost(processUrl, body, {}, true, false)
}



export function cacheRemHandler(action, id) {
    let remCacheUrl = url.remCacheUrl.replace(":action", action).replace(":id", id)
    post(remCacheUrl, {}, {}, true, false).then(
        response => {
            Message({
                message: "缓存清理成功:" + response.data.result,
                type: "success"
            })
        }
    )
}

export function exportPostman(filename, apiName = [],) {
    let exportUrl = url.exportPostmanFileUrl
    post(exportUrl, {name: filename, url: baseUrl, api_name: apiName}, {responseType: 'blob'}, true, false).then(
        res => {
            const blob = new Blob([res.data]);//处理文档流
            const fileName = filename + '.json';
            const elink = document.createElement('a');
            elink.download = fileName;
            elink.style.display = 'none';
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 释放URL 对象
            document.body.removeChild(elink);
        }
    )
}

export function getNull() {
    return {
        _id: "",
        api_name: "",
        group_name: "",
        description: "",
        data: {
            method: "",
            encoding: "",
            url: "",
            "Content-Type": "",
            body: "",
            argument: [],
        },
        request_function: {},
        response_function: {},
        format: [],
        cache: {
            key: "",
            time: 0
        },
    }
}
